package pri.black.dogview.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import pri.black.dogview.domain.Admin;
import pri.black.dogview.exception.BizException;

public class SecurityContextUtils {
    public static Admin admin(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        if (authentication == null) {
            throw new BizException();
        }
        if (authentication instanceof TokenAuthentication tokenAuthentication) {
            UserDetails userDetails = tokenAuthentication.getUserDetails();
            if (userDetails instanceof AdminDetails adminDetails) {
                Admin admin = adminDetails.getAdmin();
                return admin;
            }
        }
        throw new BizException();
    }
}
